home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / c / gifsave.zip / gifsave.c < prev    next >
C/C++ Source or Header  |  1992-09-26  |  32KB  |  1,204 lines

  1.  
  2. /**************************************************************************
  3.  *
  4.  *  FILE:           GIFSAVE.C
  5.  *
  6.  *  MODULE OF:      GIFSAVE
  7.  *
  8.  *  DESCRIPTION:    Routines to create a GIF-file. See GIFSAVE.DOC for
  9.  *                  a description . . .
  10.  *
  11.  *                  The functions were originally written using Borland's
  12.  *                  C-compiler on an IBM PC -compatible computer, but they
  13.  *                  are compiled and tested on SunOS (Unix) as well.
  14.  *
  15.  *  WRITTEN BY:     Sverre H. Huseby
  16.  *                  Bjoelsengt. 17
  17.  *                  N-0468 Oslo
  18.  *                  Norway
  19.  *
  20.  *                  sverrehu@ifi.uio.no
  21.  *
  22.  *  LAST MODIFIED:  26/9-1992, v1.0, Sverre H. Huseby
  23.  *                    * Version 1.0, no modifications
  24.  *
  25.  **************************************************************************/
  26.  
  27.  
  28.  
  29.  
  30.  
  31. #include <stdlib.h>
  32. #include <stdio.h>
  33.  
  34. #include "gifsave.h"
  35.  
  36.  
  37.  
  38.  
  39.  
  40. /**************************************************************************
  41.  *                                                                        *
  42.  *                       P R I V A T E    D A T A                         *
  43.  *                                                                        *
  44.  **************************************************************************/
  45.  
  46.  
  47. typedef unsigned Word;          /* At least two bytes (16 bits) */
  48. typedef unsigned char Byte;     /* Exactly one byte (8 bits) */
  49.  
  50.  
  51.  
  52. /*========================================================================*
  53.  =                                                                        =
  54.  =                             I/O Routines                               =
  55.  =                                                                        =
  56.  *========================================================================*/
  57.  
  58. static FILE *OutFile;           /* File to write to */
  59.  
  60.  
  61.  
  62. /*========================================================================*
  63.  =                                                                        =
  64.  =                      Routines to write a bit-file                      =
  65.  =                                                                        =
  66.  *========================================================================*/
  67.  
  68. static Byte Buffer[256];        /* There must be one to much !!! */
  69. static int  Index,              /* Current byte in buffer */
  70.             BitsLeft;           /* Bits left to fill in current byte. These
  71.                                 /* are right-justified */
  72.  
  73.  
  74.  
  75. /*========================================================================*
  76.  =                                                                        =
  77.  =                Routines to maintain an LZW-string table                =
  78.  =                                                                        =
  79.  *========================================================================*/
  80.  
  81. #define RES_CODES 2
  82.  
  83. #define HASH_FREE 0xFFFF
  84. #define NEXT_FIRST 0xFFFF
  85.  
  86. #define MAXBITS 12
  87. #define MAXSTR (1 << MAXBITS)
  88.  
  89. #define HASHSIZE 9973
  90. #define HASHSTEP 2039
  91.  
  92. #define HASH(index, lastbyte) (((lastbyte << 8) ^ index) % HASHSIZE)
  93.  
  94. static Byte *StrChr = NULL;
  95. static Word *StrNxt = NULL,
  96.             *StrHsh = NULL,
  97.             NumStrings;
  98.  
  99.  
  100.  
  101. /*========================================================================*
  102.  =                                                                        =
  103.  =                               Main routines                            =
  104.  =                                                                        =
  105.  *========================================================================*/
  106.  
  107. typedef struct {
  108.     Word LocalScreenWidth,
  109.          LocalScreenHeight;
  110.     Byte GlobalColorTableSize : 3,
  111.          SortFlag             : 1,
  112.          ColorResolution      : 3,
  113.          GlobalColorTableFlag : 1;
  114.     Byte BackgroundColorIndex;
  115.     Byte PixelAspectRatio;
  116. } ScreenDescriptor;
  117.  
  118. typedef struct {
  119.     Byte Separator;
  120.     Word LeftPosition,
  121.          TopPosition;
  122.     Word Width,
  123.          Height;
  124.     Byte LocalColorTableSize : 3,
  125.          Reserved            : 2,
  126.          SortFlag            : 1,
  127.          InterlaceFlag       : 1,
  128.          LocalColorTableFlag : 1;
  129. } ImageDescriptor;
  130.  
  131. static int  BitsPrPrimColor,    /* Bits pr primary color */
  132.             NumColors;          /* Number of colors in color table */
  133. static Byte *ColorTable = NULL;
  134. static Word ScreenHeight,
  135.             ScreenWidth,
  136.             ImageHeight,
  137.             ImageWidth,
  138.             ImageLeft,
  139.             ImageTop,
  140.             RelPixX, RelPixY;         /* Used by InputByte() -function */
  141. static int  (*GetPixel)(int x, int y);
  142.  
  143.  
  144.  
  145.  
  146.  
  147.  
  148.  
  149.  
  150.  
  151.  
  152. /**************************************************************************
  153.  *                                                                        *
  154.  *                   P R I V A T E    F U N C T I O N S                   *
  155.  *                                                                        *
  156.  **************************************************************************/
  157.  
  158.  
  159. /*========================================================================*
  160.  =                                                                        =
  161.  =                         Routines to do file IO                         =
  162.  =                                                                        =
  163.  *========================================================================*/
  164.  
  165. /*-------------------------------------------------------------------------
  166.  *
  167.  *  NAME:           Create()
  168.  *
  169.  *  DESCRIPTION:    Creates a new file, and enables referencing using the
  170.  *                  global variable OutFile. This variable is only used
  171.  *                  by these IO-functions, making it relatively simple to
  172.  *                  rewrite file IO.
  173.  *
  174.  *  PARAMETERS:     filename - Name of file to create
  175.  *
  176.  *  RETURNS:        GIF_OK       - OK
  177.  *                  GIF_ERRWRITE - Error opening the file
  178.  *
  179.  */
  180. static int Create(char *filename)
  181. {
  182.     if ((OutFile = fopen(filename, "wb")) == NULL)
  183.         return GIF_ERRCREATE;
  184.  
  185.     return GIF_OK;
  186. }
  187.  
  188.  
  189.  
  190.  
  191.  
  192. /*-------------------------------------------------------------------------
  193.  *
  194.  *  NAME:           Write()
  195.  *
  196.  *  DESCRIPTION:    Output bytes to the current OutFile.
  197.  *
  198.  *  PARAMETERS:     buf - Pointer to buffer to write
  199.  *                  len - Number of bytes to write
  200.  *
  201.  *  RETURNS:        GIF_OK       - OK
  202.  *                  GIF_ERRWRITE - Error writing to the file
  203.  *
  204.  */
  205. static int Write(void *buf, unsigned len)
  206. {
  207.     if (fwrite(buf, sizeof(Byte), len, OutFile) < len)
  208.         return GIF_ERRWRITE;
  209.  
  210.     return GIF_OK;
  211. }
  212.  
  213.  
  214.  
  215.  
  216.  
  217. /*-------------------------------------------------------------------------
  218.  *
  219.  *  NAME:           WriteByte()
  220.  *
  221.  *  DESCRIPTION:    Output one byte to the current OutFile.
  222.  *
  223.  *  PARAMETERS:     b - Byte to write
  224.  *
  225.  *  RETURNS:        GIF_OK       - OK
  226.  *                  GIF_ERRWRITE - Error writing to the file
  227.  *
  228.  */
  229. static int WriteByte(Byte b)
  230. {
  231.     if (putc(b, OutFile) == EOF)
  232.         return GIF_ERRWRITE;
  233.  
  234.     return GIF_OK;
  235. }
  236.  
  237.  
  238.  
  239.  
  240.  
  241. /*-------------------------------------------------------------------------
  242.  *
  243.  *  NAME:           WriteWord()
  244.  *
  245.  *  DESCRIPTION:    Output one word (2 bytes with byte-swapping, like on
  246.  *                  the IBM PC) to the current OutFile.
  247.  *
  248.  *  PARAMETERS:     w - Word to write
  249.  *
  250.  *  RETURNS:        GIF_OK       - OK
  251.  *                  GIF_ERRWRITE - Error writing to the file
  252.  *
  253.  */
  254. static int WriteWord(Word w)
  255. {
  256.     if (putc(w & 0xFF, OutFile) == EOF)
  257.         return GIF_ERRWRITE;
  258.  
  259.     if (putc((w >> 8), OutFile) == EOF)
  260.         return GIF_ERRWRITE;
  261.  
  262.     return GIF_OK;
  263. }
  264.  
  265.  
  266.  
  267.  
  268.  
  269. /*-------------------------------------------------------------------------
  270.  *
  271.  *  NAME:           Close()
  272.  *
  273.  *  DESCRIPTION:    Close current OutFile.
  274.  *
  275.  *  PARAMETERS:     None
  276.  *
  277.  *  RETURNS:        Nothing
  278.  *
  279.  */
  280. static void Close(void)